package au.com.bus4u.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 页面数据对象
 * 
 */
public class PageData extends HashMap implements Map {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 数据map
	 */
	Map map = null;
	
	HttpServletRequest request;
	
	/**
	 * 有参构造器
	 * @param request 请求对象
	 */
	public PageData(HttpServletRequest request) {

		this.request = request;
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		String name = "";
		String value = "";
		
		if (request != null) {
			this.log.info("getRequestURI :   == " + request.getRequestURI());
			this.log.info("getServletPath :   == " + request.getServletPath());
			this.log.info("getRequestedSessionId :   == " + request.getRequestedSessionId());
		}
		
		while (entries.hasNext()) {
			Entry entry = (Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (valueObj == null) {
				value = "";
			} else if ((valueObj instanceof String[])) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			this.log.info("参数  " + name + " == " + value);
			returnMap.put(name, value);
		}
		this.map = returnMap;

	}
	
	/**
	 * 无参构造
	 */
	public PageData() {
		this.map = new HashMap();
	}
	
	/**
	 * 获取参数值
	 */
	@Override
	public Object get(Object key) {
		Object obj = null;
		if ((this.map.get(key) instanceof Object[])) {
			Object[] arrayOfObject = (Object[]) this.map.get(key);
		} else {
			obj = this.map.get(key);
		}
		return obj;
	}

	public Integer getInt(Object key) {
		return Integer.parseInt(get(key).toString());
	}
	
	public String getString(Object key) {
		return (String) get(key);
	}
	
	@SuppressWarnings("unchecked")	//告诉编译器忽略 unchecked 警告信息，如使用List，ArrayList等未进行参数化产生的警告信息。
	@Override
	public Object put(Object key, Object value) {
		return this.map.put(key, value);
	}
	
	@Override
	public Object remove(Object key) {
		return this.map.remove(key);
	}
	
	/**
	 * 清空请求数据
	 */
	public void clear() {
		this.map.clear();
	}
	
	/**
	 * 判断是否存在请求参数
	 */
	public boolean containsKey(Object key) {
		return this.map.containsKey(key);
	}
	
	/**
	 * 判断是否存在请求值
	 */
	public boolean containsValue(Object value) {
		return this.map.containsValue(value);
	}
	
	/**
	 * 获取entry集合
	 */
	public Set entrySet() {
		return this.map.entrySet();
	}
	
	/**
	 * 判断参数集合是否为空
	 */
	public boolean isEmpty() {
		return this.map.isEmpty();
	}

	public Set keySet() {
		return this.map.keySet();
	}
	
	/**
	 * 添加集合
	 */
	@SuppressWarnings("unchecked")
	public void putAll(Map t) {
		this.map.putAll(t);
	}
	
	/**
	 * 获取请求集合的大小
	 */
	public int size() {
		return this.map.size();
	}
	
	/**
	 * 获取请求参数集合
	 */
	public Collection values() {
		return this.map.values();
	}

	private class pageExt {
		private pageExt() {
			getClass();
		}
	}
	
	
	
    /** 
     * 获取用户真实IP地址，不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址, 
*  
     * 可是，如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值，究竟哪个才是真正的用户端的真实IP呢？ 
     * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。 
     *  
     * 如：X-Forwarded-For：192.168.1.110, 192.168.1.120, 192.168.1.130, 
     * 192.168.1.100 
     *  
     * 用户真实IP为： 192.168.1.110 
     *  
     * @param request 
     * @return 
     */  
    public String getIpAddress() {  
    	String ip = request.getHeader("x-forwarded-for"); 
//        System.err.println("x-forwarded-for ip: " + ip);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {  
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
//            System.out.println("Proxy-Client-IP ip: " + ip);
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
//            System.out.println("WL-Proxy-Client-IP ip: " + ip);
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_CLIENT_IP");  
//            System.out.println("HTTP_CLIENT_IP ip: " + ip);
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
//            System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("X-Real-IP");  
//            System.out.println("X-Real-IP ip: " + ip);
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
//            System.out.println("getRemoteAddr ip: " + ip);
        } 
//        System.err.println("获取客户端ip: " + ip);
        return ip;  
    } 
}
